﻿using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace Pay.DTO
{
	/// <summary>
	/// 退款请求
	/// </summary>
	public class RefundRequestDto : IValidatableObject
	{
		/// <summary>
		/// 应用appId,此字段还可以放在header请求头、query查询、Authorization特定请求头;以上位置必传一个
		/// </summary>
		public string AppId { get; set; }

		/// <summary>
		/// 退款类型
		/// </summary>
		[Required]
		[EnumDataType(typeof(PayCategoryEnum))]
		public PayCategoryEnum PayCategory { get; set; }

		/// <summary>
		/// 商户订单号
		/// </summary>
		[Required]
		[StringLength(32, MinimumLength = 6)]
		public string OutTradeNo { get; set; }

		/// <summary>
		/// 退款单号
		/// </summary>
		[Required]
		[StringLength(64, MinimumLength = 1)]
		public string RefundNo { get; set; }

		/// <summary>
		/// 订单总金额,元,精度0.01
		/// </summary>
		[Required]
		public decimal Total { get; set; }

		/// <summary>
		/// 退款金额，元，精度0.01
		/// </summary>
		[Required]
		public decimal RefundAmount { get; set; }

		/// <summary>
		/// 退款原因
		/// </summary>
		[StringLength(80)]
		public string Reason { get; set; }

		/// <summary>
		/// 退款回调URL
		/// </summary>
		[Required]
		[StringLength(256)]
		public string NotifyUrl { get; set; }

        /// <summary>
        /// 请求基URL
        /// </summary>
        [JsonIgnore]
        public string? BaseUrl { get; set; }

        /// <summary>
        /// Validate
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			if (RefundAmount > Total)
			{
				yield return new ValidationResult("退款金额大于订单总金额");
				yield break;
			}

			if (string.IsNullOrWhiteSpace(Reason))
				Reason = "正常退款";
		}
	}
}